package com.SmartCare.service.impl.message;

import com.SmartCare.common.dto.MessageDTO;
import com.SmartCare.mapper.message.MsgBindCareHomeMapper;
import com.SmartCare.mapper.message.MsgBindElderMapper;
import com.SmartCare.pojo.message.MsgBindCareHome;
import com.SmartCare.pojo.message.MsgBindElder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.SmartCare.common.Result;
import com.SmartCare.mapper.role.UserMapper;
import com.SmartCare.pojo.role.User;
import com.SmartCare.service.message.IdentityVerificationService;
import com.SmartCare.mapper.message.IdentityVerificationMapper;
import com.SmartCare.pojo.message.IdentityVerification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 身份审核接口
 */
@Service
public class IdentityVerificationServiceImpl extends ServiceImpl<IdentityVerificationMapper, IdentityVerification> implements IdentityVerificationService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private MsgBindCareHomeMapper msgBindCareHomeMapper;

    @Resource
    private MsgBindElderMapper msgBindElderMapper;

    @Override
    public Result getListByCareHomeName(String name) {
        QueryWrapper<IdentityVerification> qw = new QueryWrapper<>();
        qw.eq("care_home_name", name);
        qw.orderByDesc("create_time");
        return Result.success(list(qw));
    }

    @Override
    public Result updateMsg(IdentityVerification msg) {
        //更新消息状态
        updateById(msg);
        if (msg.getStatus() == 1) {
            //修改用户角色
            User user = userMapper.selectById(msg.getUserId());
            if (user == null) {
                return Result.error("用户不存在");
            }
            user.setRole(msg.getRole());
            user.setCareHomeName(msg.getCareHomeName());
            userMapper.updateById(user);
        }
        return Result.success();
    }

    //待完善
    @Override
    public Result updateUserInfo(Long userId, Integer identity, String careHomeName) {

        IdentityVerification
                .builder()
                .role(identity)
                .careHomeName(careHomeName)
                .status(0)
                .build();

        return Result.success();
    }

    @Override
    public Result addNewVerification(String careHomeName, Long userId, Integer role) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }
        IdentityVerification verification = IdentityVerification.builder()
                .role(role)
                .userId(userId)
                .name(user.getName())
                .careHomeName(careHomeName)
                .build();
        save(verification);
        return Result.success();
    }

    @Override
    public Result getAllMessagesByUserId(Long userId) {
        //先判空
        User user = userMapper.selectById(userId);
        if (user == null) {
            return Result.error("用户不存在");
        }
        //定义返回结果
        List<MessageDTO> dtos = new ArrayList<>();
        //获取角色申请消息
        List<IdentityVerification> list1 = list(new QueryWrapper<IdentityVerification>().eq("user_id", userId));
        for (IdentityVerification iv : list1) {
            //逐个转成dto
            MessageDTO dto = MessageDTO.builder()
                    .id(iv.getId())
                    .userId(iv.getUserId())
                    .careHomeName(iv.getCareHomeName())
                    .role(iv.getRole())
                    .status(iv.getStatus())
                    .createTime(iv.getCreateTime())
                    .type("角色绑定")
                    .build();
            //添加至返回结果中
            dtos.add(dto);
        }
        //获取养老院绑定消息
        List<MsgBindCareHome> list2 = msgBindCareHomeMapper.selectList(new QueryWrapper<MsgBindCareHome>().eq("user_id", userId));
        for (MsgBindCareHome mbc : list2) {
            //逐个转成dto
            MessageDTO dto = MessageDTO.builder()
                    .id(mbc.getId())
                    .careHomeName(mbc.getCareHomeName())
                    .userId(mbc.getUserId())
                    .status(mbc.getStatus())
                    .createTime(mbc.getCreateTime())
                    .type("养老院绑定")
                    .build();
            //添加至返回结果中
            dtos.add(dto);
        }
        //获取老人绑定消息
        List<MsgBindElder> list3 = msgBindElderMapper.selectList(new QueryWrapper<MsgBindElder>().eq("user_id", userId));
        for (MsgBindElder mbe : list3) {
            //逐个转成dto
            MessageDTO dto = MessageDTO.builder()
                    .id(mbe.getId())
                    .careHomeName(mbe.getCareHomeName())
                    .userId(mbe.getUserId())
                    .status(mbe.getStatus())
                    .createTime(mbe.getCreateTime())
                    .elderId(mbe.getElderId())
                    .type("老人绑定")
                    .build();
            //添加至返回结果中
            dtos.add(dto);
        }

        return Result.success(dtos);
    }
}
